<template>
  <div class="list-box" ref="listBox">
    <!-- 搜索区-->
    <div class="list-search">
      <el-button type="primary" class="list-search-right" @click="dialogForm = true">新增供应商</el-button>
    </div>
    <!--列表区-->
    <div class="list-table">
      <el-table :data="tableData" border :style="{width: '100%'}" v-loading="loading" :height="clientHeight">
        <el-table-column prop="userId" label="ID" width="100"></el-table-column>
        <el-table-column prop="trueName" label="单位"></el-table-column>
        <el-table-column prop="contactUser" label="姓名" width="200"></el-table-column>
        <el-table-column prop="contactTel" label="联系方式" width="200"></el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="onClickOpenChange(scope.row)">修改</el-button>
            <el-button type="danger" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--新增\修改弹窗-->
    <el-dialog title="供应商信息" :visible.sync="dialogForm" @close="onClickClose" class="list-dialog" :close-on-click-modal="false">
      <el-form :model="form" label-width="100px" :rules="rules" ref="setUser">
        <el-form-item label="账号" class="list-dialog-input" prop="userName" v-if="!form.userId">
          <el-input v-model="form.userName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单位" class="list-dialog-input" prop="trueName">
          <el-input v-model="form.trueName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" class="list-dialog-input" prop="contactUser">
          <el-input v-model="form.contactUser" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" class="list-dialog-input" prop="contactTel">
          <el-input v-model="form.contactTel" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="onClickSetUser">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
    export default {
      name: "userList",
      props:{
        clientHeight:{
          type:String
        }
      },
      data(){
        return{
          input:'',
          tableData:[],
          form:{},
          loading:false,//是否开启加载
          dialogForm:false,//新增、修改窗口显示
          rules:{
            userName:[{ required: true, message: '请输入账号', trigger: 'blur' }],
            trueName:[{ required: true, message: '请输入账号', trigger: 'blur' }],
            contactUser:[{ required: true, message: '请输入账号', trigger: 'blur' }],
            contactTel:[{ required: true, message: '请输入账号', trigger: 'blur' }],
          }
        }
      },
      created(){
        this.getUserList();
      },
      methods:{
        //获取供应商列表
        getUserList(){
          this.loading = true;
          this.$axios.getUser().then(res=>{
            console.log(res)
            if (res.data.code==='0'){
              this.tableData = res.data.data;
              let t = setTimeout(()=>{
                this.loading = false;
                clearTimeout(t);
              },500)
            }else{
              this.$message.error('获取列表失败');
            }
          })
        },
        //打开修改窗口
        onClickOpenChange(row){
          this.form = JSON.parse(JSON.stringify(row));
          this.dialogForm = true;
        },
        //关闭新增、修改窗口
        onClickClose(){
          this.form = {};
        },
        //新增、修改提交
        onClickSetUser(){
          this.$refs['setUser'].validate((valid)=>{
            if(valid){
              let form = this.form;
              this.$axios.setUser({
                "userId":form.userId?form.userId:'0',
                "userName":form.userName?form.userName:'',//账号
                "key":form.userId?'':this.guid(),
                "trueName":form.trueName,
                "contactUser":form.contactUser,
                "contactTel":form.contactTel
              }).then((res)=>{
                if (res.data.code==='0'){
                  this.$message.success(this.form.userId?'修改成功！':'添加成功！');
                  this.getUserList();
                  this.$emit('changeList');
                  this.dialogForm = false;
                }else {
                  this.$message.error('添加或修改失败！');
                }
              })
            }
          })
        },
        //自动生成key
        guid() {
          return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
          })
        },
      }
    }
</script>

<style scoped lang="scss">
  .list-box{
    background: #fff;
    height: 100%;
    width: 100%;
    padding: 30px;
    .list-search{

    }
    .list-table{
      width: 100%;
      margin: 30px 0;
    }
    .list-pagination{
      float: right;
    }
    .list-dialog-input{
      max-width: 500px;
    }
  }
</style>
